package com.baizhiedu.basic.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @Author: robert.song
 * @Date: Created in 2021/8/6
 */
public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
//        将spring的应用上下文对象存储到ServletContext域当中
        ServletContext servletContext = servletContextEvent.getServletContext();
        ApplicationContext app = new ClassPathXmlApplicationContext(servletContext.getInitParameter("autoContextConfigLocation"));
        servletContext.setAttribute("app1", app);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
